﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Client.Models
{
    public class BaseCmd
    {
        #region 登录设备协议
        /// <summary>
        /// 登录设备模板
        /// </summary>    
        public class LoginModel
        {
            public string DEVMAC { get; set; }
            public string DEVTYPE { get; set; }
            public string NAME { get; set; }

            public override string ToString()
            {
                string retStr = "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n";
                retStr += "<INPUT>\r\n<DEVMAC>" + DEVMAC + "</DEVMAC>\r\n<DEVTYPE>" + DEVTYPE + "</DEVTYPE>\r\n<NAME>" + NAME + "</NAME>\r\n</INPUT>";
                return retStr;
            }
            public void ResolveXML(string xmlString)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlString))
                        throw new Exception("参数不能为空！");
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlString);
                    XmlElement xl = xDoc.DocumentElement;
                    var xList = xl.GetElementsByTagName("DEVMAC"); ;
                    DEVMAC = xList.Count < 1 ? "" : xList[0].InnerText;
                    xList = xl.GetElementsByTagName("DEVTYPE");
                    DEVTYPE = xList.Count < 1 ? "" : xList[0].InnerText;
                    xList = xl.GetElementsByTagName("NAME");
                    NAME = xList.Count < 1 ? "" : xList[0].InnerText;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        public class LoginReturn
        {
            public int CODE { get; set; }
            public string MESS { get; set; }
            public string SQMY { get; set; }
            public void ResolveXML(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        throw new Exception("数据不能为空！");
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xl = xDoc.DocumentElement;
                    if (null == xl)
                        throw new Exception("数据格式不正确！");
                    var xList = xl.GetElementsByTagName("RETURN");
                    if (xList.Count < 1)
                        throw new Exception("数据格式不正确！");
                    var xl1 = (XmlElement)xList[0];
                    xList = xl1.GetElementsByTagName("CODE");
                    CODE = xList.Count < 1 ? -1 : Convert.ToInt32(xList[0].InnerText);
                    xList = xl1.GetElementsByTagName("MESS");
                    MESS = xList.Count < 1 ? "" : xList[0].InnerText;
                    xList = xl.GetElementsByTagName("DATA");
                    if (xList.Count < 1)
                        throw new Exception("数据格式不正确！");
                    xl1 = (XmlElement)xList[0];
                    xList = xl1.GetElementsByTagName("SQMY");
                    SQMY = xList.Count < 1 ? "" : xList[0].InnerText;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        #endregion
        #region 3.5商品分类编码下载协议
        /// <summary>
        /// 商品分类编码下载协议包模型
        /// </summary>
        public class TaxCodingMessageModel
        {
            public string NSRSBH { get; set; }
            public string FJH { get; set; }
            public string SPBMBBH { get; set; }
            public string SPBM { get; set; }

            public override string ToString()
            {
                string rStr = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
                rStr += string.Format("<INPUT>\r\n<NSRSBH>{0}</NSRSBH>\r\n<FJH>{1}</FJH>\r\n<SPBMBBH>{2}</SPBMBBH>\r\n<SPBM>{3}</SPBM></INPUT>", NSRSBH, FJH, SPBMBBH, SPBM);
                return rStr;
            }
        }
        /// <summary>
        /// 税收分类编码模型
        /// </summary>
        public class TaxCodingInfoModel
        {
            /// <summary>
            /// 商品编码
            /// </summary>
            public string SPBM { get; set; }
            /// <summary>
            /// 货物和劳务名称
            /// </summary>
            public string SPMC { get; set; }
            /// <summary>
            /// 说明
            /// </summary>
            public string SM { get; set; }
            /// <summary>
            /// 增值税税率
            /// </summary>
            public float ZZSSL { get; set; }
            /// <summary>
            /// 关键字
            /// </summary>
            public string GJZ { get; set; }
            /// <summary>
            /// 是否可归并上一级
            /// </summary>
            public bool KGB { get; set; }
            /// <summary>
            /// 版本号
            /// </summary>
            public string BBH { get; set; }
            /// <summary>
            /// 可用状态
            /// </summary>
            public string KYZT { get; set; }
            /// <summary>
            /// 增值税特殊管理
            /// </summary>
            public string ZZSTSGL { get; set; }
            /// <summary>
            /// 增值税政策依据
            /// </summary>
            public string ZZSZCYJ { get; set; }
            /// <summary>
            /// 增值税特殊内容代码
            /// </summary>
            public string ZZSTSNRDM { get; set; }
            /// <summary>
            /// 消费税管理
            /// </summary>
            public string XFSGL { get; set; }
            /// <summary>
            /// 消费税政策依据
            /// </summary>
            public string XFSZCYJ { get; set; }
            /// <summary>
            /// 消费税特殊内容代码
            /// </summary>
            public string XFSTSNRDM { get; set; }
            /// <summary>
            /// 统计局编码或国民行业代码
            /// </summary>
            public string TJJBM { get; set; }
            /// <summary>
            /// 海关进出口商品品目
            /// </summary>
            public string HGJCKSPPM { get; set; }
            /// <summary>
            /// 启用时间
            /// </summary>
            public DateTime QYSM { get; set; }
            /// <summary>
            /// 过渡期截止时间
            /// </summary>
            public DateTime GDQJZSJ { get; set; }
            /// <summary>
            /// 更新时间
            /// </summary>
            public DateTime GXSJ { get; set; }

            public static TaxCodingInfoModel[] ResolveXML(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        return null;
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xl = xDoc.DocumentElement;
                    var xList = xl.GetElementsByTagName("DATA");
                    if (xList.Count < 1)
                        return null;
                    var xl1 = (XmlElement)xList[0];
                    xList = xl1.GetElementsByTagName("SPBMXX");
                    if (xList.Count < 1)
                    {
                        return null;
                    }
                    string xData = xList[0].InnerText;
                    xData = CryptUtil.CompressUtil.DeCompressGzip(xData, Encoding.UTF8);
                    xDoc.LoadXml(xList[0].InnerXml);
                    xl = xDoc.DocumentElement;
                    xList = xl.GetElementsByTagName("SPBM");
                    if (xList.Count < 1)
                        throw new Exception("数据格式不正确！");
                    xl = (XmlElement)xList[0];
                    xList = xl.GetElementsByTagName("SPBMGXSL");
                    int elCount = xList.Count > 0 ? Convert.ToInt32(xList[0].InnerText) : 0;
                    if (elCount < 1)
                        return null;
                    xList = xl.GetElementsByTagName("SPBMXX");
                    if (xList.Count != elCount)
                        throw new Exception("数据格式不正确！");
                    TaxCodingInfoModel[] tInfo = new TaxCodingInfoModel[elCount];
                    for (int i = 0; i < xList.Count; i++)
                    {
                        var xn = xList[i];
                        tInfo[i] = new TaxCodingInfoModel
                        {
                            BBH = xn.SelectSingleNode("BBH").InnerText,
                            GDQJZSJ = Convert.ToDateTime(xn.SelectSingleNode("GZQJZSJ").InnerText),
                            GJZ = xn.SelectSingleNode("GJZ").InnerText,
                            GXSJ = Convert.ToDateTime(xn.SelectSingleNode("GXSJ").InnerText),
                            HGJCKSPPM = xn.SelectSingleNode("HGJCKSPPM").InnerText,
                            KGB = Convert.ToBoolean(xn.SelectSingleNode("KGB").InnerText),
                            KYZT = xn.SelectSingleNode("KYZT").InnerText,
                            QYSM = Convert.ToDateTime(xn.SelectSingleNode("QYSM").InnerText),
                            SM = xn.SelectSingleNode("SM").InnerText,
                            SPBM = xn.SelectSingleNode("SPBM").InnerText,
                            SPMC = xn.SelectSingleNode("SPMC").InnerText,
                            TJJBM = xn.SelectSingleNode("TJJBM").InnerText,
                            XFSGL = xn.SelectSingleNode("XFSGL").InnerText,
                            XFSTSNRDM = xn.SelectSingleNode("XFSTSNRDM").InnerText,
                            XFSZCYJ = xn.SelectSingleNode("XFSZCYJ").InnerText,
                            ZZSSL = Convert.ToSingle(xn.SelectSingleNode("ZZSSL").InnerText),
                            ZZSTSGL = xn.SelectSingleNode("ZZSTSGL").InnerText,
                            ZZSTSNRDM = xn.SelectSingleNode("ZZSTSNRDM").InnerText,
                            ZZSZCYJ = xn.SelectSingleNode("ZZSZCYJ").InnerText
                        };
                    }
                    return tInfo;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        /// <summary>
        /// 优惠政策模型
        /// </summary>
        public class TaxCodingPolicyModel
        {
            public string YHZCMC { get; set; }
            public string SL { get; set; }

            public static TaxCodingPolicyModel[] ResolveXML(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        throw new Exception("无效的数据！");
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xl = xDoc.DocumentElement;
                    var xList = xl.GetElementsByTagName("DATA");
                    if (xList.Count < 1)
                        return null;
                    var xl1 = (XmlElement)xList[0];
                    xList = xl1.GetElementsByTagName("SPBMXX");
                    if (xList.Count < 1)
                        return null;
                    string xData = xList[0].InnerText;
                    xData = CryptUtil.CompressUtil.DeCompressGzip(xData);
                    xDoc.LoadXml(xData);
                    xl = xDoc.DocumentElement;
                    xList = xl.GetElementsByTagName("YHZC");
                    if (xList.Count < 1)
                        throw new Exception("数据格式不正确！");
                    xl1 = (XmlElement)xList[0];
                    xList = xl1.GetElementsByTagName("ZZSYHZC");
                    if (xList.Count < 1)
                        return null;
                    TaxCodingPolicyModel[] tcp = new TaxCodingPolicyModel[xList.Count];
                    for (int i = 0; i < xList.Count; i++)
                    {
                        var xn = xList[0];
                        tcp[i] = new TaxCodingPolicyModel
                        {
                            SL = xn.SelectSingleNode("SL").InnerText,
                            YHZCMC = xn.SelectSingleNode("YHZCMC").InnerText
                        };
                    }
                    return tcp;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        /// <summary>
        /// 商品编码明细返回数据模型
        /// </summary>
        public class TaxCodingReturn
        {
            public int CODE { get; set; }
            public string MESS { get; set; }
            public string SPBMBBH { get; set; }
            public string SPBM { get; set; }
            public TaxCodingInfoModel[] SPBMXX { get; set; }
            public TaxCodingPolicyModel[] YHZC { get; set; }
            public int SPBMGXSL { get; set; }

            public void ResolveXML(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        throw new Exception("无效的数据！");
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xl = xDoc.DocumentElement;
                    var xList = xl.GetElementsByTagName("RETURN");
                    if (xList.Count < 1)
                        throw new Exception("数据格式不正确！");
                    var xl1 = (XmlElement)xList[0];
                    xList = xl1.GetElementsByTagName("CODE");
                    CODE = xList.Count > 0 ? Convert.ToInt32(xList[0].InnerText) : -1;
                    xList = xl1.GetElementsByTagName("MESS");
                    MESS = xList.Count > 0 ? xList[0].InnerText : "";
                    xList = xl.GetElementsByTagName("DATA");
                    if (xList.Count < 1)
                        throw new Exception("数据格式不正确！未找到数据段");
                    xl1 = (XmlElement)xList[0];
                    xList = xl1.GetElementsByTagName("SPBMBBH");
                    SPBMBBH = xList.Count > 0 ? xList[0].InnerText : "";
                    xList = xl1.GetElementsByTagName("SPBM");
                    SPBM = xList.Count > 0 ? xList[0].InnerText : "";
                    //商品编码明细
                    SPBMXX = TaxCodingInfoModel.ResolveXML(xmlData);
                    //优惠政策
                    YHZC = TaxCodingPolicyModel.ResolveXML(xmlData);
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        #endregion
        #region 3.1税控信息查询协议
        public class SKQueryRequestModel
        {
            public string NSRSBH { get; set; }
            public string FJH { get; set; }
            public override string ToString()
            {
                return string.Format("<?xml version=\"1.0\" encoding=\"GBK\"?>{0}<INPUT>{0}<NSRSBH>{1}</NSRSBH>{0}<FJH>{2}</FJH>{0}</INPUT>", Environment.NewLine, NSRSBH, FJH);
            }
        }
        public class SKQueryOutputModel
        {
            public SKQueryReturnModel ReturnStatus { get; set; }
            public SKQueryDataModel ReturnData { get; set; }
            public void ResolveXml(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        throw new Exception("数据不能为空！");
                    ReturnStatus = new SKQueryReturnModel();
                    ReturnStatus.ResolveXml(xmlData);
                    if (ReturnStatus.CODE != "000000")
                        return;
                    ReturnData = new SKQueryDataModel();
                    ReturnData.ResolveXml(xmlData);
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        public class SKQueryReturnModel
        {
            public string CODE { get; set; }
            public string MESS { get; set; }
            public void ResolveXml(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        throw new Exception("数据不能为空！");
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xe = xDoc.DocumentElement;
                    XmlNodeList xList = xe.GetElementsByTagName("RETURN");
                    if (null == xList || xList.Count < 1)
                        throw new Exception("数据格式不正确！");
                    XmlNode xn = xList[0];
                    CODE = xn.SelectSingleNode("CODE").InnerText;
                    MESS = xn.SelectSingleNode("MESS").InnerText;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        public class SKQueryDataModel
        {
            public string JSPBH { get; set; }
            public string NSRSBH { get; set; }
            public string NSRMC { get; set; }
            public string SWJGDM { get; set; }
            public string SWJGMC { get; set; }
            public string FXDQBH { get; set; }
            public DateTime DQSZ { get; set; }
            public DateTime QYSJ { get; set; }
            public string BBH { get; set; }
            public string FJH { get; set; }
            public string QYLX { get; set; }
            public string QTKZXX { get; set; }
            public int PZCOUNT { get; set; }
            public SKQueryInvoiceTypeModel[] PZSQXX { get; set; }

            public void ResolveXml(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        return;
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xe = xDoc.DocumentElement;
                    XmlNodeList xList = xe.GetElementsByTagName("DATA");
                    if (null == xList || xList.Count < 1)
                        throw new Exception("解析失败！");
                    XmlNode xn = xList[0];
                    JSPBH = xn.SelectSingleNode("JSPBH").InnerText;
                    NSRSBH = xn.SelectSingleNode("NSRSBH").InnerText;
                    NSRMC = xn.SelectSingleNode("NSRMC").InnerText;
                    SWJGDM = xn.SelectSingleNode("SWJGDM").InnerText;
                    SWJGMC = xn.SelectSingleNode("SWJGMC").InnerText;
                    FXDQBH = xn.SelectSingleNode("FXDQBH").InnerText;
                    DQSZ = DateTime.ParseExact(xn.SelectSingleNode("DQSZ").InnerText, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                    QYSJ = DateTime.ParseExact(xn.SelectSingleNode("QYSJ").InnerText, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
                    BBH = xn.SelectSingleNode("BBH").InnerText;
                    FJH = xn.SelectSingleNode("FJH").InnerText;
                    QYLX = xn.SelectSingleNode("QYLX").InnerText;
                    QTKZXX = xn.SelectSingleNode("QTKZXX").InnerText;
                    PZSQXX = SKQueryInvoiceTypeModel.ResolveXml(xmlData);
                    PZCOUNT = PZSQXX == null ? 0 : PZSQXX.Length;
                }
                catch (Exception)
                {

                    throw;
                }
            }

        }
        public class SKQueryInvoiceTypeModel
        {
            public string FPZL { get; set; }
            public DateTime SSRQ { get; set; }
            public decimal DZKPXE { get; set; }
            public string SCJZRQ { get; set; }
            public string LXSX { get; set; }
            public decimal LXXE { get; set; }
            public decimal LXSYJE { get; set; }
            public string LXKZXX { get; set; }
            public int SLCOUNT { get; set; }
            public SKQueryInvoiceRateModel[] SQSLXX { get; set; }
            public static SKQueryInvoiceTypeModel[] ResolveXml(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        return null;
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xe = xDoc.DocumentElement;
                    XmlNodeList xList = xe.GetElementsByTagName("DATA");
                    if (null == xList || xList.Count < 1)
                        return null;
                    xe = (XmlElement)xList[0];
                    XmlNode xn = xe.SelectSingleNode("PZSQXX");
                    int slCount = Convert.ToInt32(xn.Attributes["COUNT"].Value);
                    if (slCount < 1)
                        return null;
                    xe = (XmlElement)xn;
                    xList = xe.GetElementsByTagName("PZSQ");
                    if (null == xList || xList.Count < 1)
                        return null;
                    if (xList.Count != slCount)
                        return null;
                    SKQueryInvoiceTypeModel[] st = new SKQueryInvoiceTypeModel[slCount];
                    for (int i = 0; i < xList.Count; i++)
                    {
                        xn = xList[i];
                        st[i] = new SKQueryInvoiceTypeModel
                        {
                            DZKPXE = Convert.ToDecimal(xn.SelectSingleNode("DZKPXE").InnerText),
                            FPZL = xn.SelectSingleNode("FPZL").InnerText,
                            LXKZXX = xn.SelectSingleNode("LXKZXX").InnerText,
                            LXSX = xn.SelectSingleNode("LXSX").InnerText,
                            LXSYJE = Convert.ToDecimal(xn.SelectSingleNode("LXSYJE").InnerText),
                            LXXE = Convert.ToDecimal(xn.SelectSingleNode("LXXE").InnerText),
                            SCJZRQ = xn.SelectSingleNode("SCJZRQ").InnerText,
                            SSRQ = DateTime.ParseExact(xn.SelectSingleNode("SSRQ").InnerText, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture)
                        };
                        int fCount = Convert.ToInt32(xn.SelectSingleNode("SQSLXX").Attributes["COUNT"].Value);
                        st[i].SQSLXX = SKQueryInvoiceRateModel.ResolveXml(xmlData);
                        if (fCount != st[i].SQSLXX.Length)
                        {
                            st[i].SQSLXX = null;
                            fCount = -1;
                        }
                    }
                    return st;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        public class SKQueryInvoiceRateModel
        {
            public decimal SL { get; set; }
            public static SKQueryInvoiceRateModel[] ResolveXml(string xmlData)
            {
                try
                {
                    if (string.IsNullOrEmpty(xmlData))
                        return null;
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.LoadXml(xmlData);
                    XmlElement xe = xDoc.DocumentElement;
                    XmlNodeList xList = xe.GetElementsByTagName("SQSLXX");
                    if (null == xList || xList.Count < 1)
                        return null;
                    xe = (XmlElement)xList[0];
                    xList = xe.GetElementsByTagName("SQSL");
                    if (null == xList || xList.Count < 1)
                        return null;
                    SKQueryInvoiceRateModel[] ski = new SKQueryInvoiceRateModel[xList.Count];
                    for (int i = 0; i < xList.Count; i++)
                    {
                        ski[i] = new SKQueryInvoiceRateModel
                        {
                            SL = Convert.ToDecimal(xList[i].SelectSingleNode("SL").InnerText)
                        };
                    }
                    return ski;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
    }
}
#endregion